<?php

namespace app\api\controller\admin;

use app\common\controller\Api;
use think\Db;

/**
 * 首页接口
 */
class Order extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];
    public function index()
    {
        $this->success('请求成功66555');
    }

    /**
     * 查看
     */
    public function gindex()
    {

        $params = $this->request->param();
        $action =$params['action'];
        if($action=='apiindex'){
            $data= self::apiindex();
            return $data;
        }elseif ($action=='apiadd'){
            self::apiadd();
        }elseif ($action=='apiedit'){
            self::apiedit();
        }elseif ($action=='apidel'){
            self::apidel();
        }



    }

    /**
     * 查看
     */
    public function apiindex()
    {

        $params = $this->request->param();
        $pageSize =$params['pageSize'];
        $pageNum =$params['pageNum'];
        $name =isset($params['name']) ? $params['name'] : '';
        $sort=isset($params['sort']) ? $params['sort'] : 'id';
        $order=isset($params['order']) ? $params['order'] : 'desc';
        $systemFlag=isset($params['systemFlag']) ? $params['systemFlag'] : '';
        $where=[];
        if ($systemFlag){
            $where['id']  = array('like',  '%'.$systemFlag."%");
        }
        if ($name){
            $where['username']  = array('like',  '%'.$name."%");
        }
//        $pageNum=1;
//        $pageSize=1;
//        $sort='id';
//        $order='desc';
//halt($where);
        $pageNum=($pageNum-1)*$pageSize;
        $list = Db::table('fa_g_order')
            ->where($where)
            ->field(['password', 'salt', 'token'], true)
            ->order($sort, $order)
            ->limit($pageNum,$pageSize)
            ->select();
//        halt($list);
        $total= Db::table('fa_g_order')
            ->where($where)
            ->field(['password', 'salt', 'token'], true)
            ->order($sort, $order)
            ->limit(1,2)
            ->count();
        foreach ($list as $k => &$v) {
            $username = Db::table('fa_g_user')->where('id', $v['user_id'])->field('username')->find();
            $goodsname = Db::table('fa_g_goods')->where('id', $v['goods_id'])->field('name')->find();
            $v['username'] = $username['username'];
            $v['goodsname'] = $goodsname['name'];
        }
        unset($v);
        $result = array("total" => $total, "rows" => $list);
        return json($result);

    }
    /**
     * add
     */
    public function apiadd()
    {
        $post = $this->request->post();
        unset($post['action']);
        $post['createtime']=time();
        $list=Db::table('fa_g_order')->insertGetId($post);

        $this->success($list);

    }
    /**
     * up
     */

    public function apiedit()
    {
        $post = $this->request->post();
        unset($post['action']);
//        halt($post);
        $post['updatetime']=time();
        $list=Db::table('fa_g_order')->update($post);
        $this->success($list);

    }
    public function apidel()
    {
        $post = $this->request->post();
        unset($post['action']);
        $list=Db::table('fa_g_order')->delete($post);
        $this->success($list);

    }
}
